var nav;
var time = 0;
$(function(){
	if($("#nav").length>0){
		nav = $("#nav").val();
	}
	$.ajax({
		url:BASE_PATH+'/index/rec.do',
		type:'get',
		dataType:'json',
		data:{
			nv:nav
		},
		success:function(data){
			createRec(data);
		}
	});
	$.ajax({
		url:BASE_PATH+'/toutiao/weare.do',
		type:'get',
		dataType:'json',
		success:function(data){
			createWear(data);
		}
	});
	$.ajax({
		url:BASE_PATH+'/index/gettags.do',
		type:'get',
		dataType:'json',
		success:function(data){
			createTagPic(data);
		}
	});
	$(".tab-title").on('mouseover','a',function(){
		$(".tab-title li").removeClass("cur");
		$(this).parent().addClass("cur");
		var name = $(this).attr("name");
		$(".tab-content ul").hide();
		$("#"+name).show();
	});
	$(".tab-title").on('click','a',function(){
		var name = $(this).attr("name");
		$(".tab-pane").hide();
		$("#"+name).show();
	});
	getToutiao();
	setInterval(function(){
		getToutiao();
	},1000*60);
	
	$("#tophot_fresh").on('click',function(){
		getToutiao();
	});
});

function getToutiao(){
	$.ajax({
		url:BASE_PATH+'/toutiao/tophot.do',
		data:{
			time:time
		},
		type:'get',
		dataType:'json',
		success:function(data){
			time = data['time'];
			createTopHot(data['data']);
		}
	});
}

function createTopHot(data){
	var html = "";
	for(var i = 0; i < data.length; i++){
		html+="<li><a target='_blank' href='"+data[i]['url']+"'>"+cutWords(16,data[i]['title'])+"</a></li>";
	}
	$("#tophot").html(html);
}

function createWear(data){
	var html = "";
	for(var i = 0; i < data.length; i++){
		html+="<li><span class='label "+sorted_color[i]+" title-sort'>"+(i+1)+"</span><a target='_blank' href='"+data[i]['url']+"'>"+cutWords(16,data[i]['title'])+"</a></li>";
	}
	$("#weare").html(html);
}

function createRec(data){
	for(var k in data){
		var html = "";
		if(data[k]){
			for(var i = 0; i < data[k].length; i++){
				html+="<li><span class='label "+sorted_color[i]+" title-sort'>"+(i+1)+"</span><a href='"+BASE_PATH+"/article/"+data[k][i].id+".html'>"+cutWords(16,data[k][i].title)+"</a></li>";
			}
		}
		$("#"+k).html(html);
	}
}

function createTagPic(data){
	var html = "<div id='tagscloud'>";
	if(data){
		var k = 1;
		for(var i in data){
			html+="<a href='"+BASE_PATH+"/tag/1/"+data[i].id+".html"+"' class='tagc"+k+"'>"+data[i].tagName+"</a>";
			k++;
		}
	}
	html+="</div><script src='"+JS_PATH+"tagscloud.js'></script><script>tagpic()</script>"
	$(".tagpic").html(html);
}
